package com.sl.pay.handler.alipay;

import cn.hutool.core.convert.Convert;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.sl.pay.entity.TradingEntity;
import com.sl.pay.enums.TradingStateEnum;
import com.sl.pay.handler.NativePayHandler;
import com.sl.transport.common.exception.SLException;
import org.springframework.stereotype.Component;

/**
 * 支付宝实现类
 */
@Component("aliNativePayHandler")
public class AliNativePayHandler implements NativePayHandler {

    /**
     * 创建二维码
     *
     * @param tradingEntity 交易单
     * @throws SLException
     */
    @Override
    public void createDownLineTrading(TradingEntity tradingEntity) throws SLException {
        // 1.设置支付宝公共参数
        Factory.setOptions(AlipayConfig.getConfig());

        // 2.发起交易
        AlipayTradePrecreateResponse response = null;
        try {
            response = Factory.Payment.FaceToFace().preCreate(tradingEntity.getMemo(),
                    Convert.toStr(tradingEntity.getTradingOrderNo()),
                    Convert.toStr(tradingEntity.getTradingAmount())
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ResponseChecker.success(response)) {
            // 支付连接
            assert response != null;
            String qrCode = response.getQrCode();

            // 将链接变为二维码  用户扫码支付
            System.out.println(qrCode);
            tradingEntity.setTradingState(TradingStateEnum.FKZ);
        }
    }


}
